//
//  DoctorViewController.m
//  WE
//
//  Created by iMacOne on 15/11/20.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "DoctorViewController.h"
#import "DoctorAddressCell.h"
#import "Size.h"
#import "DoctorProficientCell.h"
#import "CommentNumCell.h"
#import "CommentTextCell.h"
#import "ToolUtil.h"
#import "CommentControl.h"
#import "AuthenticateControl.h"
#import "EMMallSectionView.h"
#import "CheckInControl.h"
#import "FocusonViewController.h"
#import "AppointmentViewController.h"
#define HEADCOLOR [UIColor colorWithRed:145/255.0 green:115/255.0 blue:94/255.0 alpha:1.0]
@interface DoctorViewController ()


{
    UIImageView *imageV;
//    UIView *_headerView;
}
@property (nonatomic, assign)BOOL isOpen;
@property (nonatomic, assign)NSInteger selectedIndex;
@property (nonatomic, strong)UIButton * contentBtn;
@property (nonatomic, strong)UIImageView * arrowImage;
@property (nonatomic, strong)UIImageView * aImgeView;
@property (nonatomic, strong)UILabel * contentLbl;
@property (nonatomic, strong)UILabel * titleLbl;
@end

@implementation DoctorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"医生";
    self.view.backgroundColor = [UIColor whiteColor];
   // self.navigationController.navigationBar.hidden = YES;
    [self.view addSubview:self.tableView];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    self.isOpen = NO;
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:170/255. green:160/255. blue:100/255. alpha:1.0];
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WindowWidth, 200*Height)];
    _headerView.backgroundColor = HEADCOLOR;
    UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectview.frame = CGRectMake(0, 0, WindowWidth, 200*Height);
    [_headerView addSubview:effectview];
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 110*Height, WindowWidth, 20*Height)];
    nameLabel.text = @"王实 主治医师";
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.font = FONT(14);
    [_headerView addSubview:nameLabel];
    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 140*Height, (WindowWidth - 1)/3, 15*Height)];
    numberLabel.textColor = [UIColor whiteColor];
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.font = FONT(12);
    numberLabel.text = @"22";
    [_headerView addSubview:numberLabel];
    [ToolUtil createVerticalLine:140*Height superView:_headerView lift:(WindowWidth - 1)/3 height:40*Height];
    UILabel *numberLabel1 = [[UILabel alloc] initWithFrame:CGRectMake((WindowWidth - 1)/3 + 0.5, 140*Height, (WindowWidth - 1)/3, 15*Height)];
    numberLabel1.textColor = [UIColor whiteColor];
    numberLabel1.text = @"暂无";
    numberLabel1.font = FONT(12);
    numberLabel1.textAlignment = NSTextAlignmentCenter;
    [_headerView addSubview:numberLabel1];
    [ToolUtil createVerticalLine:140*Height superView:_headerView lift:(WindowWidth - 1)/3*2+0.5 height:40*Height];
    UILabel *numberLabel2 = [[UILabel alloc] initWithFrame:CGRectMake((WindowWidth - 1)/3*2 + 1, 140*Height, (WindowWidth - 1)/3, 15*Height)];
    numberLabel2.textColor = [UIColor whiteColor];
    numberLabel2.font = FONT(12);
    numberLabel2.textAlignment = NSTextAlignmentCenter;
    numberLabel2.text = @"9.8";
    [_headerView addSubview:numberLabel2];
    
    UILabel *orderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 170*Height, (WindowWidth - 1)/3, 15*Height)];
    orderLabel.text = @"预约量";
    orderLabel.textAlignment = NSTextAlignmentCenter;
                                                                   
    orderLabel.textColor = [UIColor whiteColor];
    orderLabel.font = FONT(10);
    [_headerView addSubview:orderLabel];
    
    UILabel *referLabel = [[UILabel alloc] initWithFrame:CGRectMake((WindowWidth - 1)/3 + 0.5, 170*Height, (WindowWidth - 1)/3, 15*Height)];
    referLabel.textAlignment = NSTextAlignmentCenter;
    referLabel.textColor = [UIColor whiteColor];
    referLabel.text = @"咨询量";
    referLabel.font = FONT(10);
    [_headerView addSubview:referLabel];
    
    UILabel *reviewLabel = [[UILabel alloc] initWithFrame:CGRectMake((WindowWidth - 1)/3*2 + 1, 170*Height, (WindowWidth - 1)/3, 15*Height)];
    reviewLabel.textColor = [UIColor whiteColor];
    reviewLabel.font = FONT(10);
    reviewLabel.textAlignment = NSTextAlignmentCenter;
    reviewLabel.text = @"患者好评";
    [_headerView addSubview:reviewLabel];
    self.tableView.tableHeaderView= _headerView;
    [self creatBottomView];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
//    [_headerView addSubview:imageV];
    imageV = [[UIImageView alloc] initWithFrame:CGRectMake((WindowWidth - 60*Height)/2, 30*Height, 70*Height, 70*Height)];
    imageV.layer.masksToBounds = YES;
    imageV.layer.cornerRadius = 30;
    imageV.image = [UIImage imageNamed:@"111"];
    [_headerView addSubview:imageV];
}


- (void)viewDidDisappear:(BOOL)animated{
    [imageV removeFromSuperview];
}

- (void)pressBackButton{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)creatBottomView
{
    NSArray * arr1 = @[@"患者报到",@"关注",@"预约挂号",@"分享"];
    NSArray * arr2 = @[@"iconfont-xinjie",@"iconfont-7",@"",@"iconfont-fenxiang"];
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, WindowHeight - 50, WindowWidth, 50)];
    _bottomView.backgroundColor = [UIColor whiteColor];
    for (int i = 0 ; i < 4; i++) {
        UIImageView * aImageView = [[UIImageView alloc] initWithFrame:CGRectMake((WindowWidth /4 - [ToolUtil autoWidth:arr1[i] width:0 height:30 num:12] - 30)/2, 5, 30, 30)];
//        aImageView.backgroundColor = DEBUG_CLOLR;
        aImageView.image = [UIImage imageNamed:arr2[i]];
        UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(aImageView.frame) + 5, 5, [ToolUtil autoWidth:arr1[i] width:0 height:30 num:12], 30)];
        lbl.text = arr1[i];
        lbl.textColor = CONTENTCOLOR;
//        lbl.backgroundColor = DEBUG_CLOLR;
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.font = FONT(12);
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake( i * WindowWidth / 4 + i*0.5 ,5, WindowWidth /4-1.5/3, 50);
            //            btn.layer.borderWidth = 0.5;
        btn.tag = 400 + i;
        [btn addTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.titleLabel.font = FONT(12);
        
        [ToolUtil createVerticalLine:0 superView:_bottomView lift:CGRectGetMaxX(btn.frame) height:50];
        [btn addSubview:aImageView];
        [btn addSubview:lbl];
        
        if (i == 2) {
            [aImageView removeFromSuperview];
            lbl.frame = CGRectMake((btn.frame.size.width - lbl.frame.size.width)/2, lbl.frame.origin.y + 5, lbl.frame.size.width, lbl.frame.size.height);
            btn.backgroundColor = [UIColor colorWithRed:0.2595 green:0.2965 blue:0.7388 alpha:1.0];
            btn.frame = CGRectMake(btn.frame.origin.x, 0, btn.frame.size.width, btn.frame.size.height);
            lbl.textColor = [UIColor whiteColor];
        }
        
        [self.bottomView addSubview:btn];
    }
         
    [ToolUtil createLine:0 superView:_bottomView lift:0 width:WindowWidth hight:1 color:GRAYCOLOR];
    
    [self.view addSubview:self.bottomView];
}
- (void)clickbtn:(UIButton *)btn
{
    if (btn.tag == 400) {
//        [self.navigationController pushViewController:[[AuthenticateControl alloc] init] animated:YES];
        [self.navigationController pushViewController:[[CheckInControl alloc] init] animated:YES];
    }else if (btn.tag == 401){
        
//        [self.navigationController pushViewController:[[FocusonViewController alloc] init] animated:YES];
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"暂未登录" preferredStyle:(UIAlertControllerStyleAlert)];
        
//        UIAlertAction * CameraAction = [UIAlertAction actionWithTitle:@"相机" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
//        }];
        [self presentViewController:alertController animated:YES completion:^{
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        
    }else if (btn.tag == 403){
        [ToolUtil share:@"微医" content:@"好用么" url:@"URL啊" image:nil view:self.view control:self];
    }else if (btn.tag == 402){
        [self.navigationController pushViewController:[[AppointmentViewController alloc] init] animated:YES];
    }
    
}
- (void)doClickBtn:(NSInteger)number cell:(UITableViewCell *)cell
{
    if ([cell isKindOfClass:[CommentNumCell class]]) {
        [self.navigationController pushViewController:[[CommentControl alloc] init] animated:YES];
    }
}
- (UIView *)creatView
{
    
    UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 50)];
    self.aImgeView = [[UIImageView alloc] initWithFrame:CGRectMake(margin_10 * Width, margin_10 * Height, 20* Width , 20* Height)];
    self.aImgeView.layer.masksToBounds = YES;
    self.aImgeView.layer.cornerRadius = 10;
    self.aImgeView.backgroundColor = DEBUG_CLOLR;
    
    self.titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_aImgeView.frame) + margin_5, _aImgeView.frame.origin.y, margin_64, 20)];
    _titleLbl.font = FONT(14);
    _titleLbl.text = @"擅长";
    
    self.contentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _contentBtn.frame = CGRectMake(WindowWidth - 60, 2, 40, 30);
    [_contentBtn addTarget:self action:@selector(doClickButton) forControlEvents:UIControlEventTouchUpInside];
    int icon_w = 40 * 0.4, icon_h = 40*0.4*0.63, icon_x = (40 - icon_w) / 2, icon_y = (30 - icon_h) / 2;
    self.arrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(icon_x, icon_y , icon_w, icon_h)];
    if (!self.isOpen) {
        self.arrowImage.image = [UIImage imageNamed:@"arrow_bottom.png"];
    }else if (self.isOpen){
        self.arrowImage.image = [UIImage imageNamed:@"arrow_up.png"];
    }

    [_contentBtn addSubview:_arrowImage];

    self.contentLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_titleLbl.frame), WindowWidth - 20, margin_34)];
    _contentLbl.font = FONT(12);
    _contentLbl.text = @"前段时间播出的 <<医馆笑谈>> 中前段时间播出的 <<医馆笑谈>> 中前段时间播出的 <<医馆笑谈>> 中前段时间播出的 <<医馆笑谈>> 中前段时间播出的";
    _contentLbl.numberOfLines = 2;
    _contentLbl.textColor = [UIColor grayColor];
    [aView addSubview:_titleLbl];
    [aView addSubview:_aImgeView];
    [aView addSubview:_contentBtn];
    return aView;
}

- (void)doClickButton
{
    if (!self.isOpen) {
        self.isOpen = YES;
        self.arrowImage.image = [UIImage imageNamed:@"arrow_up.png"];
    }else if (self.isOpen){
        self.arrowImage.image = [UIImage imageNamed:@"arrow_bottom.png"];
        self.isOpen = NO;

    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];

}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0 ,WindowWidth , WindowHeight - 50 ) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        

    }
    return _tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1 && self.isOpen
         == NO) {
        return 0;
    }else if (section == 1 && self.isOpen == YES){
        return 1;
    }else if (section == 2){
        return 1;
    }else if (section == 0){
        return 1;
    }
    return 3;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return [self creatView];
    }
    EMMallSectionView *sectionView =  [EMMallSectionView showWithName:nil];
    sectionView.sign = @"1";
    sectionView.tableView = self.tableView;
    sectionView.section = section;
    return sectionView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }else if (section == 2){
        return 0;
    }else if(section == 1){
        return 0;
    }
        
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
       return 80;
    }else if (indexPath.section == 3){
        return [CommentTextCell cellHeight];
    }
    return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        DoctorAddressCell * cell = [[DoctorAddressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 1){
        if (self.isOpen) {
            DoctorProficientCell * cell = [[DoctorProficientCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            return cell;
        }
    }else if (indexPath.section == 2 ){
        CommentNumCell * cell = [[CommentNumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.delegate = self;
        return cell;
    }
    
    static NSString * cellIdentifier = @"CommentTextCell";
    
    CommentTextCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[CommentTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
                }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
//    DoctorViewController * doctVC = [[DoctorViewController alloc] init];
//    [self.navigationController pushViewController:doctVC animated:YES];
    
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    
//    NSLog(@"%lf",scrollView.contentOffset.y);
//    if (scrollView.contentOffset.y > -20*Height) {//如果当前位移大于缓存位移，说明scrollView向上滑动
//        //
//        float a = 60*Height - (scrollView.contentOffset.y+20*Height);
//        if (a<=30*Height) {
//            imageV.frame = CGRectMake((WindowWidth-30*Height)/2, (44-30*Height)/2, 30*Height, 30*Height);
//            imageV.layer.cornerRadius = 30*Height/2;
//            [self.navigationController.navigationBar addSubview:imageV];
//        }else if(a>30*Height){
//        
//        imageV.frame = CGRectMake((WindowWidth - a)/2, 44*Height + scrollView.contentOffset.y + 20*Height, a, a);
//            imageV.layer.cornerRadius = a/2;
//            [_headerView addSubview:imageV];
//            
//        }
//        //self.navigationController.navigationBar.hidden = NO;
//    }
// 
//}


@end
